/**
 * 
 */
package cn.sundat.web;

import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.sundat.model.School;
import cn.sundat.model.School_link;
import cn.sundat.service.SchoolService;

/**
 * @author XIN
 *
 */
public class LinkAction extends ActionSupport implements ModelDriven<School_link> {
	
	@Resource(name="schoolService")
	private SchoolService schoolService;
	School_link school_link=new School_link();
	@Override
	public School_link getModel() {
		// TODO Auto-generated method stub
		return school_link;
	}
	public String add() throws Exception{
		String uuid=ServletActionContext.getRequest().getParameter("uuid");
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
//			Set school_links=new HashSet<>();
			School school = schoolService.findSchoolById(uuid);
			Set school_links=school.getSchool_links();
			school_links.add(school_link);
			school.setSchool_links(school_links);
			schoolService.editSchool(school);
			writer.print("1");
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			writer.print("0");
		}
		writer.flush();
		writer.close();
		return NONE;
	}
	public String del() throws Exception{
		//uuid=sch_id
		String uuid=ServletActionContext.getRequest().getParameter("uuid");
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			schoolService.delSchool_link(schoolService.findSchool_linkBySchlink_id(uuid));
			writer.print("1");
			
		} catch (Exception e) {
			// TODO: handle exception
			writer.print("0");
		}
		writer.flush();
		writer.close();
		return NONE;
	}
	public String edit() throws Exception{
		String uuid=ServletActionContext.getRequest().getParameter("uuid");
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			schoolService.editSchool_link(school_link);
			writer.print("1");
			
		} catch (Exception e) {
			// TODO: handle exception
			writer.print("0");
		}
		writer.flush();
		writer.close();
		return NONE;
	}
	public String find() throws Exception{
		String uuid=ServletActionContext.getRequest().getParameter("uuid");
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			String jsonString = JSON.toJSONString(schoolService.findSchool_linkById(uuid));
			writer.print(jsonString);
			
		} catch (Exception e) {
			// TODO: handle exception
			writer.print("0");
		}
		writer.flush();
		writer.close();
		return NONE;
	}
	public String findBySch_id() throws Exception{
		String uuid=ServletActionContext.getRequest().getParameter("uuid");
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		try {
			String jsonString = JSON.toJSONString(schoolService.findSchool_linkBySchlink_id(uuid));
			writer.print(jsonString);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			writer.print("0");
		}
		writer.flush();
		writer.close();
		return NONE;
	}

}
